package com.example.coursedemo.controller;

import com.example.coursedemo.jiekou.portdeobject;
import com.example.coursedemo.object.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

@Controller
public class StudentController {
    Student student=new Student();
    private Model model;



    @RequestMapping("/")
    //
    public String welcome() {
        return "welcome";
    }
    @PostMapping("/welcome")
    public String informatian() {
        return "studentInfo";
    }
    @PostMapping("/information")
    public String lesson(@RequestParam("name")String name, @RequestParam("sno")Integer sno, @RequestParam("grade")Integer grade, @RequestParam("major")String major, @RequestParam("college")String college, @Valid Student deobject, BindingResult result) {
        if(result.hasErrors()) {
            return "studentInfo";
        }
        student.setName(name);
        student.setSno(sno);
        student.setGrade(grade);
        student.setMajor(major);
        student.setCollege(college);

        return "course";
    }

    @Autowired
    portdeobject portdobject;

    @PostMapping("/lesson")
    public String result(Model model, @RequestParam("course")String course, @Valid Student deobject, BindingResult result) {
        student.setCourse(course);
        model.addAttribute("student",student);
        portdobject.add(student);

        return "result";
    }
}

